//
//  BookListTableViewController.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/11.
//

import UIKit

class BookListTableViewController: UITableViewController {
    
    var keys = [String]()
    var books = [String:[[String:String]]]()
    var user:String=""

    override func viewDidLoad() {
        super.viewDidLoad()
        BookTable.initTable()
        //这个函数运行一次就可以注释掉
        BookTable.insertAllBookFromWeb()
        getKey()
        getBook()
        let xib = UINib(nibName: "BookTableViewCell", bundle: nil)
        tableView.register(xib, forCellReuseIdentifier: "bookcell")
        tableView.rowHeight = 170

    }
    override func viewWillAppear(_ animated: Bool) {
        print("44444")
    }


    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return keys.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = keys[section]
        let books = books[key]!
        return books.count
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return keys[section]
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "bookcell", for: indexPath) as! BookTableViewCell
        
        let key = keys[indexPath.section]
        let booklist = books[key]!
        cell.bookTitle.text = booklist[indexPath.row]["title"]
        cell.bookPrice.text = booklist[indexPath.row]["price"]
        cell.bookImage.downloadAsyncFrom(url: booklist[indexPath.row]["pic"]!)
        
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let key = keys[indexPath.section]
        let booklist = books[key]!
        let bookid = booklist[indexPath.row]["id"]!
        
        let controller:BookDetailViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "bookdetail") as! BookDetailViewController
        
        controller.bookid = bookid
        self.show(controller, sender: self)
    }
    
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    
    func getKey()
    {
        let queryResult = BookTable.getKind()
        for i in queryResult!{
            let a = i["kind"]! as! String
            keys.append(a)
        }
    }
    func getBook()
    {
        for kind in keys
        {
            let queryResult = BookTable.getBook(kind)
            let a = queryResult as! [[String:String]]
            books.updateValue(a, forKey: kind)
        }
    }
    



    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */



}
